package com.cuit.riskassessment.service.common.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cuit.riskassessment.dao.common.ModuleMapper;
import com.cuit.riskassessment.dao.project.ProjectMapper;
import com.cuit.riskassessment.service.common.ModuleService;


@Service("ModuleServiceObject")
public class ModuleServiceImp implements ModuleService {

	@Autowired
	@Resource(name = "moduleMapper")
	private ModuleMapper moduleMapper;

	@Override
	public Map getModuleList(String id) {
		
		List result = new ArrayList();
		
		if(id.equals("root")){
			id = "000000" ;
		}
		
		//一级
		List list_level1 = moduleMapper.getModuleList(id) ;
		
		for(int i = 0 , len = list_level1.size() ; i < len ; i ++){
			Map map = new HashMap() ;
			Map m = (Map)list_level1.get(i) ;
			map.put("text", m.get("VC_MODULE_NAME")) ;
			map.put("myhref", m.get("VC_MODULE_HREF")) ;
			map.put("contenttype", m.get("VC_CONTENT_TYPE")) ;
			map.put("id", m.get("VC_MODULE_CD")) ;
			
			result.add(map) ;
		}
		
		for(int i = 0 , len = result.size() ; i < len ; i ++){
			Map m = (Map)result.get(i) ;
			//二级
			List list_level2 = moduleMapper.getModuleList(m.get("id").toString()) ;
			if(list_level2.size() > 0){
				List result_level2 = new ArrayList() ;
				for(int j = 0 , l = list_level2.size() ; j < l ; j ++){
					Map map2 = new HashMap() ;
					Map m2 = (Map)list_level2.get(j) ;
					map2.put("text", m2.get("VC_MODULE_NAME")) ;
					map2.put("myhref", m2.get("VC_MODULE_HREF")) ;
					map2.put("contenttype", m2.get("VC_CONTENT_TYPE")) ;
					map2.put("id", m2.get("VC_MODULE_CD")) ;
					result_level2.add(map2) ;
					
					//三级
					List list_level3 = moduleMapper.getModuleList(m2.get("VC_MODULE_CD").toString()) ;
					if(list_level3.size() > 0){
						List result_level3 = new ArrayList() ;
						for(int k = 0 , n = list_level3.size() ; k < n ; k ++){
							Map map3 = new HashMap() ;
							Map m3 = (Map)list_level3.get(k) ;
							map3.put("text", m3.get("VC_MODULE_NAME")) ;
							map3.put("myhref", m3.get("VC_MODULE_HREF")) ;
							map3.put("contenttype", m3.get("VC_CONTENT_TYPE")) ;
							map3.put("id", m3.get("VC_MODULE_CD")) ;
							map3.put("leaf", true) ;
							result_level3.add(map3) ;
						}
						map2.put("children", result_level3) ;
					}else{
						map2.put("leaf", true) ;
					}
				}
				m.put("children", result_level2) ;
			}else{
				m.put("leaf", true) ;
			}
		}
		
		Map result_m = new HashMap() ;
		result_m.put("root", result) ;
		
		return result_m ;
	}
	
	
}

